<template>
    <div class="container2">
        <div class="search-box">
            <input v-model="searchKey"  type="search" id="search">
            <button @click="searchByHand">搜索</button>
            <!-- <button @click="submitAddress" style="margin-left: 2px">确认</button> -->
            <div class="tip-box" id="searchTip"></div>
            <!-- <div>
                {{lng}}---{{lat}} ----{{markers}}
            </div> -->
        </div>
        <el-amap class="amap-box"
            :amap-manager="amapManager"
            :vid="'amap-vue'"
            :zoom="zoom"
            :plugin="plugin"
            :center="center"
            :events="events" >
            <!-- 标记 -->
            <el-amap-marker v-for="(marker, index) in markers" :position="marker" :key="index"></el-amap-marker>
        </el-amap>
    </div>
</template>

<script>
    
    import {AMapManager, lazyAMapApiLoaderInstance} from 'vue-amap'

    let amapManager = new AMapManager()
    export default {
        props: {

            address: {
                type: String,
                required: true
            },
            //
            lng: {
                type: Number,
                required: true
            },
            //
            lat: {
                type: Number,
                required: true
            },
        },
        data() {
            let self = this
            return {
                searchKey: '',
                amapManager,
                markers: [],
                searchOption: {
                    city: '全国',
                    citylimit: true
                },
                center: [121.329402, 31.228667],
                zoom: 17,
                poiPicker:null,
                loaded: false,
                markerEvent:{
                    click(e){
                        console.log(e);
                    }
                },
                events: {
                    init() {
                        lazyAMapApiLoaderInstance.load().then(() => {
                            if( self.address ){
                                console.log("回显地址")
                                self.initData();
                                self.initSearch()
                            }
                            console.log("初始化地址")
                            self.initSearch()
                        })
                    },
                    // 点击获取地址的数据
                    click(e) {
                        self.markers = []
                        let {lng, lat} = e.lnglat
                        // self.lng = lng
                        // self.lat = lat
                        self.center = [lng, lat]
                        self.markers.push([lng, lat])
                        // 这里通过高德 SDK 完成。
                        let geocoder = new AMap.Geocoder({
                            radius: 1000,
                            extensions: 'all'
                        })
                        geocoder.getAddress([lng, lat], function (status, result) {
                            if (status === 'complete' && result.info === 'OK') {
                                if (result && result.regeocode) {
                                    console.log(result.regeocode.formattedAddress)
                                    // self.address = result.regeocode.formattedAddress
                                    self.$emit( 'renderAddress', {
                                            lng : lng,
                                            lat : lat,
                                            address: result.regeocode.formattedAddress, 
                                        } 
                                    );
                                    self.searchKey = result.regeocode.formattedAddress
                                    self.$nextTick()
                                }
                            }
                        })
                    }
                },
                // 一些工具插件
                plugin: [
                    {
                        // 定位
                        pName: 'Geolocation',
                        events: {
                            init(o) {
                                // o是高德地图定位插件实例
                                o.getCurrentPosition((status, result) => {
                                    if (result && result.position) {
                                        self.$emit( 'renderAddress', {
                                                lng : result.position.lng,
                                                lat : result.position.lat,
                                                address: self.address
                                            } 
                                        );
                                        // 设置坐标
                                        self.center = [self.lng, self.lat]
                                        self.markers.push([self.lng, self.lat])
                                        // load
                                        self.loaded = true
                                        // 页面渲染好后
                                        self.$nextTick()
                                    }
                                })
                            },
                            click(e){
                                console.log(e);
                            }
                        }
                    },
                    {
                        // 工具栏
                        pName: 'ToolBar',
                        events: {
                            init(instance) {
                                console.log(instance);
                            }
                        }
                    },
                    {
                        // 鹰眼
                        pName: 'OverView',
                        events: {
                            init(instance) {
                                console.log(instance);
                            }
                        }
                    },
                    {
                        // 地图类型
                        pName: 'MapType',
                        defaultType: 0,
                        events: {
                            init(instance) {
                                console.log(instance);
                            }
                        }
                    },
                    {
                        // 搜索
                        pName: 'PlaceSearch',
                        events: {
                            init(instance) {
                                console.log(instance)
                            }
                        }
                    }
                ]
            }
        },
        methods: {
            submitAddress(){
                console.log('经纬度',this.center)
                console.log('地址',this.address)
            },
            initData(){
                this.searchKey = this.address;
                // 设置坐标
                this.center = [this.lng, this.lat]
                this.markers = []
                this.markers.push([this.lng, this.lat])
                // load
                this.loaded = true
                // 页面渲染好后
                this.$nextTick()
                console.log(this.address);
                console.log(this.lat);
                console.log(this.lng);
            },
            initSearch() {
                let vm = this
                let map = this.amapManager.getMap()
                AMapUI.loadUI(['misc/PoiPicker'], function (PoiPicker) {
                    var poiPicker = new PoiPicker({
                        input: 'search',
                        placeSearchOptions: {
                            map: map,
                            pageSize: 10
                        },
                        suggestContainer: 'searchTip',
                        searchResultsContainer: 'searchTip'
                    })
                    vm.poiPicker = poiPicker
                    // 监听poi选中信息
                    poiPicker.on('poiPicked', function (poiResult) {
                        console.log(poiResult)
                        let source = poiResult.source
                        let poi = poiResult.item
                        if (source !== 'search') {
                            poiPicker.searchByKeyword(poi.name)
                        } else {
                            poiPicker.clearSearchResults()
                            vm.markers = []
                            let lng = poi.location.lng
                            let lat = poi.location.lat
                            let address = poi.cityname + poi.adname + poi.name

                            vm.center = [lng, lat]
                            vm.markers.push([lng, lat])
                            vm.searchKey = address
                            console.log(vm.center)
                           
                            vm.$emit( 'renderAddress', {
                                    lng : lng,
                                    lat : lat,
                                    address: address, 
                                } 
                            );
                            // vm.address = address
                            
                        }
                    })
                })
            },
            searchByHand() {
                if (this.searchKey !== '') {
                    this.poiPicker.searchByKeyword(this.searchKey)
                }
            }
        }
    }


</script>


<style>

    .container2 {
        height: 350px;
        border: 1px solid #999;
    }

    .search-box {
        position: absolute;
        width: 450px;
        z-index: 5;
        left: 80px;
        top: 20px;
        height: 30px;
    }

    .search-box input {
        float: left;
        width: 59%;
        height: 100%;
        border: 1px solid #30ccc1;
        padding: 0 8px;
        outline: none;
    }

    .search-box button {
        float: left;
        width: 20%;
        height: 100%;
        background: #30ccc1;
        border: 1px solid #30ccc1;
        color: #fff;
        outline: none;
    }

    .tip-box {
        width: 100%;
        max-height: 260px;
        position: absolute;
        top: 30px;
        overflow-y: auto;
        background-color: #fff;
    }
</style>